<?php
final class LinkCJ
{
    public $advertiserId;           		// advertiser-id
    public $advertiserName;           		// advertiser-name
    public $category;                     	// category
    public $clickCommission;                // click-commission
    public $creativeHeight;                 // creative-height

    public $creativeWidth;                 	// creative-width
    public $language;                 		// language
    
    public $linkCodeHtml;                 	// link-code-html
    public $linkCodeJavascript;             // link-code-javascript
    public $description;                 	// description
    public $destination;                 	// destination
    public $linkId;                 		// link-id
    public $linkName;                 		// link-name
    public $linkType;                 		// link-type
    
    public $performanceIncentive;           // performance-incentive
    public $promotionEndDate;               // promotion-end-date
    public $promotionStartDate;             // promotion-start-date
    public $promotionType;                 	// promotion-type
    
    public $relationshipStatus;             // relationship-status
    public $saleCommission;                 // sale-commission
    public $sevenDayEpc;                 	// seven-day-epc
    public $threeMonthEpc;                 	// three-month-epc
    
    public function getCode()
    {
    	$startTags	 	= array();	
    	$startTags[1] 	= 'Coupon code "';
    	
    	$endTags	= array();
		$endTags[1]	= '"';
		
		$value 		= '';
		
		$contents	= array();
		$contents[] = $this->linkName;
		$contents[] = $this->description;
		
		foreach($contents as $content){
			foreach($startTags as $k => $startFlag){
				$endFlag = $endTags[$k];
				
				$value = $this->getCutValue($startFlag, $endFlag, $content);
				
				if(!empty($value)){
					break;
				}
			}
			
			if(!empty($value)){
				break;
			}
		}
		
		return $value;
    }
    
    public function getTrackingUrl()
    {
    	$startFlag 	= '<a href="';
		$endFlag	= '"';
		
		$value 		= '';
		$content 	= $this->linkCodeHtml;
		
		$value = $this->getCutValue($startFlag, $endFlag, $content);
	
		return $value;
    }
    
    public function getBannerUrl()
    {
    	if($this->linkType !== 'Banner')
    		return '';
    	
    	$startFlag 	= '<img src="';
		$endFlag	= '"';
		
		$value 		= '';
		$content 	= $this->linkCodeHtml;
		
		$value = $this->getCutValue($startFlag, $endFlag, $content);
	
		return $value;
    }
    
    public function getImpressionPixelUrl()
    {
    	if($this->linkType !== 'Text Link')
    		return '';
    	
    	$startFlag 	= '<img src="';
		$endFlag	= '" width="1" height="1"';
		
		$value 		= '';
		$content 	= $this->linkCodeHtml;
		
		$value = $this->getCutValue($startFlag, $endFlag, $content);
		
		return $value;
    }
    
    
    public function getFilterName()
	{
		$name = $this->linkName;
		
		$pos = strripos($name, "logo");
		if($pos > -1){
			return '';
		}
			
		$pattern[0] = "/(\d+)x(\d+)/i";
		$pattern[1] = "/(\d+) x (\d+)/i";

		$replacement 	= '';
		$name 	= preg_replace($pattern[0], $replacement, $name);
		$name 	= preg_replace($pattern[1], $replacement, $name);
		$name 	= str_ireplace ('banner', $replacement, $name);
		$name 	= str_ireplace (' logo', $replacement, $name);
		$name 	= str_ireplace ('()', $replacement, $name);
		
		$name 	= trim($name, ' ');
		$name 	= trim($name, '-');
		$name 	= trim($name, '_');
		$name 	= trim($name, ' ');
		
		return $name;
	}
	
    public function getFilterDescription()
	{
		$similar 		= 0;
		similar_text($this->linkName, $this->description, $similar);
			
		if($this->description == $this->linkName || number_format($similar, 0) > 90)
			return '';
		
		return $this->description;
	}
	
	private function getCutValue($startFlag, $endFlag, $content)
	{
		$value 	= '';
		
		$pos1 = stripos($content, $startFlag);
	
	    if( $pos1 !== false ) {
	    	$startLen = strlen($startFlag);
	    	
	    	$pos2 = stripos($content, $endFlag, $pos1 + $startLen);
	    	if( $pos2 !== false ) {
	   			$value = substr($content, $pos1 + $startLen, $pos2 - $pos1 - $startLen);
	    	}
	    }
	
		return $value;
	}
}

final class AdvertiserCJ
{
    public $advertiserId;           		// advertiser-id
    public $advertiserName;           		// advertiser-name
    public $accountStatus;                 	// account-status
    public $programUrl;                		// program-url
    public $mobileTrackingCertified;        // mobile-tracking-certified

    public $networkRank;                 	// network-rank
    public $language;                 		// language
    
    public $primaryCategory;                // primary-category
    public $performanceIncentives;          // performance-incentives
    public $actions;                 		// actions
    public $linkTypes;                 		// link-types
    
    public $relationshipStatus;             // relationship-status
    public $sevenDayEpc;                 	// seven-day-epc
    public $threeMonthEpc;                 	// three-month-epc
    
    /*
     * return category array
     */
    public function getPrimaryCategories()
    {
    	$arrCats = array();
    	
    	foreach($this->primaryCategory->children() as $k => $catName){
			$arrCats[$k] = $catName;
			
//			echo "$k : cat : $catName \n";
		}
		
		return $arrCats;
    }

	public function getChildCategory()
	{
		$cats = $this->getPrimaryCategories();
		
		if(isset($cats['child']))
			return $cats['child'];
		else
			return false;
	}
	
 	/*
     * return link type array
     */
    public function getLinkTypes()
    {
    	$arrLinkTypes = array();
    	
    	foreach($this->linkTypes->children() as $type){
			$arrLinkTypes[] = $type;
			
//			echo "type : $type \n";
		}
		
		return $arrLinkTypes;
    }
  
   	/*
     * return category array
     */
    public function getActions()
    {
    	$arrActions = array();
    	
    	foreach($this->actions->children() as $arrAction){
    		$actions = array();
    		
    		foreach($arrAction as $k=>$v){
//    			echo $k . " k : $v \n";
    			
    			if($k == 'commission'){
//    				echo $k . " 11k : $v->default \n";
    				
    				$actions[$k] = (string)$v->default;
    			}else{
    				$actions[$k] = (string)$v;
    			}
    		}

			$arrActions[] = $actions;			
		}
		
		return $arrActions;
    }      
}

?>
